<template>
  <!-- 虚线材质 一种用于绘制虚线样式几何体的材质。-->
  <div class="line-basic">
    <div id="scene"></div>
  </div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
let scene;
let camera;
let renderer;
let controls;
export default {
  data() {
    return {
      lineBox: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化
    init() {
      this.createScene(); // 创建场景
      this.createMesh(); // 创建网格模型
      this.createLight(); // 创建光源
      this.createCamera(); // 创建相机
      this.createRender(); // 创建渲染器
      this.createControls(); // 创建控件对象
      this.render(); // 渲染
    },
    // 创建场景
    createScene() {
      scene = new THREE.Scene();
      scene.background = new THREE.Color(0x111111);
      scene.fog = new THREE.Fog(0x111111, 150, 200);
    },
    createMesh() {
      const geometryBox = this.createBox(50, 50, 50);
      const materialBox = new THREE.LineDashedMaterial({
        color: 0xffaa00,
        dashSize: 3, //虚线的大小，是指破折号和间隙之和。默认值为 3。
        gapSize: 1, //间隙的大小，默认值为 1。
      });

      this.lineBox = new THREE.LineSegments(geometryBox, materialBox);
      this.lineBox.computeLineDistances();

      scene.add(this.lineBox);
    },
    createBox(width, height, depth) {
      (width = width * 0.5), (height = height * 0.5), (depth = depth * 0.5);

      const geometry = new THREE.BufferGeometry();
      const position = [];

      position.push(
        -width,
        -height,
        -depth,
        -width,
        height,
        -depth,

        -width,
        height,
        -depth,
        width,
        height,
        -depth,

        width,
        height,
        -depth,
        width,
        -height,
        -depth,

        width,
        -height,
        -depth,
        -width,
        -height,
        -depth,

        -width,
        -height,
        depth,
        -width,
        height,
        depth,

        -width,
        height,
        depth,
        width,
        height,
        depth,

        width,
        height,
        depth,
        width,
        -height,
        depth,

        width,
        -height,
        depth,
        -width,
        -height,
        depth,

        -width,
        -height,
        -depth,
        -width,
        -height,
        depth,

        -width,
        height,
        -depth,
        -width,
        height,
        depth,

        width,
        height,
        -depth,
        width,
        height,
        depth,

        width,
        -height,
        -depth,
        width,
        -height,
        depth
      );

      geometry.setAttribute(
        "position",
        new THREE.Float32BufferAttribute(position, 3)
      );

      return geometry;
    },

    // 创建光源
    createLight() {
      // 环境光
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.1); // 创建环境光
      scene.add(ambientLight); // 将环境光添加到场景

      const spotLight = new THREE.SpotLight(0xffffff); // 创建聚光灯
      spotLight.position.set(-40, 60, -10);
      spotLight.castShadow = true;
      scene.add(spotLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("scene");
      const k = element.clientWidth / element.clientHeight; // 窗口宽高比
      // PerspectiveCamera( fov, aspect, near, far )
      camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000);
      // camera.position.z = 150;
      camera.position.set(50, 70, 140);
      scene.add(camera);
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
      renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      renderer.shadowMap.enabled = true; // 显示阴影
      renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      renderer.setClearColor(0x3f3f3f, 1); // 设置背景颜色
      element.appendChild(renderer.domElement);
    },

    render() {
      // this.line.rotation.z += 0.01;
      renderer.render(scene, camera);
      requestAnimationFrame(this.render);
    },
    // 创建控件对象
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
    },
  },
};
</script>
<style>
.line-basic {
  width: 100%;
  height: 100%;
  position: relative;
}
#scene {
  width: 100%;
  height: 100%;
}
</style>

